package com.kandinfo.isbnmanage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kandinfo.base.common.constant.ResultCode;
import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.isbnmanage.domian.HomeworkTemporary;
import com.kandinfo.isbnmanage.service.HomeworkTemporaryService;
import com.kandinfo.isbnmanage.service.IsbnPageService;

/**
 * @author tangqh
 */
@Controller
@RequestMapping(value = "/isbnpage")
public class IsbnPageController {

	@Autowired
	IsbnPageService isbnPageService;
	
	@Autowired
	HomeworkTemporaryService homeworkTemporaryService;

	/**
	 * ISBN对应的图片
	 */
    @RequestMapping(value = "/index", method=RequestMethod.GET)
	public String index(Model model, String barcode, String isbnInformationId) {
		model.addAttribute("barcode", barcode);
		model.addAttribute("isbnInformationId", isbnInformationId);
		return "isbnmanage/editor/isbn_page";
	}

	/**
	 * 根据inforId查Page(已上传的图片)
	 * @param isbnInformationId
	 * @return
	 */
	@RequestMapping(value = "/getPagesView", method = RequestMethod.POST)
	public String findPagesByInforId(@RequestParam Long isbnInformationId, Model model) {
		return "isbnmanage/preview/isbn_view_page";
	}

	@RequestMapping(value = "/getPages", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto getPages(String barcode, Long isbnInformationId) {
		Map<Long, List<HomeworkTemporary>> map = new HashMap<Long, List<HomeworkTemporary>>();
		if (barcode != null && isbnInformationId != null) {
			List<HomeworkTemporary> list = homeworkTemporaryService.findPagesByInforId(barcode, isbnInformationId);
			if (!CollectionUtils.isEmpty(list)) {
				map.put(isbnInformationId, list);
			}
		} else {
			map = homeworkTemporaryService.findPagesByBarcode(barcode);
		}
		return ResultDtoFactory.createResult(ResultCode.SUCCESS, "数据查询成功", map);
	}

	@RequestMapping(value = "/getPagesData", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto getPagesData(@RequestParam Long isbnInformationId) {
		return ResultDtoFactory.createResult(ResultCode.SUCCESS, "数据查询成功", homeworkTemporaryService.findPagesByInforId(null, isbnInformationId));
	}

}
